package com.shop.front.controller.store;

import com.shop.front.vo.store.StoreCartVo;
import com.shop.domain.store.StoreCart;
import com.shop.front.request.store.StoreCartAddReq;
import com.shop.front.request.store.StoreCartUpdateCartNumReq;
import com.shop.front.service.store.StoreCartService;
import com.shop.front.shiro.UserTokenUtil;
import com.shop.util.IdSignUtil;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(description = "购物车相关", position = -1)
@RestController
@RequestMapping(value = "/storeCart", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class StoreCartController {

    @Autowired
    private StoreCartService storeCartService;


    @ApiOperation(value = "购物车列表")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreCartVo.class, message = "content"))
    @RequestMapping("/page")
    public BaseResponse page(@RequestBody @ApiIgnore Map<String, String> map) {

        Long userId = UserTokenUtil.getUser().getId();

        return storeCartService.page(userId);
    }

    @ApiOperation(value = "购物车删除")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "购物车id", paramType = "body"))
    @RequestMapping("/del")
    public BaseResponse del(@RequestBody @ApiIgnore Map<String, String> map) {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        Long userId = UserTokenUtil.getUser().getId();

        return storeCartService.del(id, userId);
    }

    @ApiOperation(value = "加入购物车")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreCart.class, message = "content"))
    @RequestMapping("/add")
    public BaseResponse add(@RequestBody @Validated StoreCartAddReq storeCartAddReq) {
        // 商品id
        Long productId = IdSignUtil.checkSign(storeCartAddReq.getProductSign());
        if(null == productId) {
            return BaseResponse.fail("请至少选择一个商品!");
        }
        storeCartAddReq.setProductId(productId);

        // 商品规格id
        Long productAttrValueId = IdSignUtil.checkSign(storeCartAddReq.getProductAttrValueSign());
        if(null == productAttrValueId) {
            return BaseResponse.fail("请至少选择一个商品规格!");
        }
        storeCartAddReq.setProductAttrValueId(productAttrValueId);

        // 拼团id
        //Long storePinkId = IdSignUtil.checkSign(storeCartAddReq.getStorePinkSign());
        //if(null != storePinkId) {
        //    storeCartAddReq.setStorePinkId(storePinkId);
        //}

        // 用户id
        Long userId = UserTokenUtil.getUser().getId();

        return storeCartService.add(userId, storeCartAddReq);
    }

    @ApiOperation(value = "修改购车商品数量")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreCart.class, message = "content"))
    @RequestMapping("/updateCartNum")
    public BaseResponse updateCartNum(@RequestBody @Validated StoreCartUpdateCartNumReq updateCartNumReq) {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        return storeCartService.updateCartNum(id, updateCartNumReq);
    }

}
